//
//  XCCollectionCellForMy.m
//  TTLestening
//
//  Created by dllo on 16/3/21.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "XCCollectionCellForMy.h"
#import "HYtableViewCellForMyTableViewCell.h"
#import "HYTableViewCell2.h"
#import "XCSaveController.h"
#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height
@interface XCCollectionCellForMy () <UITableViewDataSource, UITableViewDelegate, UIImagePickerControllerDelegate>

@property (nonatomic, strong) UIImageView *imageViewForHeaderView;

@property (nonatomic, strong) UIButton *buttonForUser;
@property (nonatomic, strong)  UILabel *labelForUser;
@property (nonatomic, strong)  UILabel *labelForSubtitle;
@property (nonatomic, strong)  UITableView *tableViewForMy;
@property (nonatomic, strong) NSMutableArray *arrForImageLike;
@property (nonatomic, strong) NSMutableArray *arrForLabelLike;
@property (nonatomic, strong) NSMutableArray *arrForLabelLikeCount;

@property (nonatomic,strong) UIView *backview;
@end

@implementation XCCollectionCellForMy

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.arrForImageLike = @[@"iconfont-xin",@"iconfont-shoucanggedan"].mutableCopy;
        self.arrForLabelLike = @[@"我的最爱",@"收藏的歌单"].mutableCopy;
        self.arrForLabelLikeCount = @[@"%ld首",@"%ld个"].mutableCopy;
        
        [self createSubViews];
        
        [self createTableView];
        
    }
    
    return self;
}


- (void)createSubViews {
    
    self.imageViewForHeaderView = [[UIImageView alloc] initWithFrame:CGRectMake(0,-64, WIDTH, WIDTH * 0.75)];
    self.imageViewForHeaderView.image = [UIImage imageNamed:@"201603160253169682.jpg"];
    
    [self.contentView addSubview:self.imageViewForHeaderView];
    
    self.backview = [[UIView alloc]initWithFrame:CGRectMake(0,0.35 * WIDTH, WIDTH, 0.4 * WIDTH - 64)];
   // self.backview.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:self.backview];
    
    
    self.buttonForUser = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonForUser.backgroundColor = [UIColor cyanColor];
    [self.backview addSubview:self.buttonForUser];
    [self.buttonForUser addTarget:self action:@selector(Userview) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonForUser setBackgroundImage:[UIImage imageNamed:@"1-140G0154154.jpg"] forState:UIControlStateNormal];
    
    self.labelForUser = [[UILabel alloc]init];
    
    [self.backview addSubview:self.labelForUser];
    self.labelForUser.text = @"死亡如风,常伴吾身";
    self.labelForUser.textColor = [UIColor whiteColor];
    self.labelForUser.font = [UIFont fontWithName:@"Arial-BoldMT" size:21];
    
    self.labelForSubtitle = [[UILabel alloc]init];
    self.labelForSubtitle.textColor = [UIColor whiteColor];
    [self.backview addSubview:self.labelForSubtitle];
    self.labelForSubtitle.text = @"累计播放歌曲0首";
    self.labelForSubtitle.font = [UIFont fontWithName:@"CourierNewPSMT" size:15];

    
   }

-(void)Userview{
    
    NSLog(@"出现个人信息页");
    
}

-(void)layoutSubviews{
    
    self.buttonForUser.frame = CGRectMake(10, 8, (0.4 * WIDTH - 64)* 0.75,(0.4 * WIDTH - 64)* 0.75);
    self.buttonForUser.layer.cornerRadius = (0.4 * WIDTH - 64)* 0.75 * 0.5;
    self.buttonForUser.layer.masksToBounds = YES;
    
    self.labelForUser.frame = CGRectMake(25 + (0.4 * WIDTH - 64)* 0.75, 12, WIDTH * 0.7, (0.4 * WIDTH - 64)* 0.3);
    
    self.labelForSubtitle.frame = CGRectMake(25 + (0.4 * WIDTH - 64)* 0.75, 20+(0.4 * WIDTH - 64)* 0.3, WIDTH * 0.7, (0.4 * WIDTH - 64)* 0.2);
    
}



- (void)createTableView {
    
    self.tableViewForMy = [[UITableView alloc] initWithFrame:CGRectMake(0, -64, WIDTH, HEIGHT + 64) style:UITableViewStylePlain];
    
    self.tableViewForMy.backgroundColor = [UIColor clearColor];
    
    self.tableViewForMy.dataSource = self;
    self.tableViewForMy.delegate = self;
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, -64, WIDTH,  WIDTH * 0.75 )];
    
    self.tableViewForMy.tableHeaderView = headerView;
    
    [self.contentView addSubview:self.tableViewForMy];
    
    [self.tableViewForMy registerClass:[HYtableViewCellForMyTableViewCell class] forCellReuseIdentifier:@"poolForMy"];
    
    UINib *nib = [UINib nibWithNibName:@"HYTableViewCell2" bundle:[NSBundle mainBundle]];
    [self.tableViewForMy registerNib:nib forCellReuseIdentifier:@"pool1"];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return 0;
    }
    
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        return (WIDTH - 30) * 0.56 + 30;
        
    } else {
        
        return 100;
        
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 1) {
        
        return 2;
        
    } else {
        
        return 1;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        HYtableViewCellForMyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForMy"];
        
        return cell;
        
    } else {
        
        NSArray *arr =  @[@"我的最爱",@"我的收藏"].mutableCopy;
        NSArray *arrtubiao = @[@"xin-1",@"shoucanggedan"].mutableCopy;
        NSArray *arrimageback  = @[@"55.jpeg",@"66.jpg"].mutableCopy;
        
        HYTableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
        cell.title.text = [arr objectAtIndex:indexPath.row];
        cell.imagetubiao.image = [UIImage imageNamed:[arrtubiao objectAtIndex:indexPath.row]];
        cell.imageback.image = [UIImage imageNamed:[arrimageback objectAtIndex:indexPath.row]];
    
        
        
        cell.selectionStyle = NO;
        
        return cell;
    }
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 1) {
        
        if (indexPath.row == 1) {
            
            // 获取该视图层最下边的VC.
            id object = [self nextResponder];
            while (![object isKindOfClass:[UIViewController class]] &&
                   object != nil) {
                object = [object nextResponder];
            }
            
            UIViewController *vc = (UIViewController*)object;
            // VideoPlayerVC 是被推出的VC.
            XCSaveController *save = [[XCSaveController alloc] init];
            [vc.navigationController pushViewController:save animated:YES];
            
        }
        
        
    }


}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.contentView.bounds.size.width - 10, self.contentView.bounds.size.height)];
    
    label.backgroundColor = [UIColor colorWithRed:0.930 green:0.929 blue:0.950 alpha:1.000];
    
    label.text = @"   我的收藏";
    
    return label;
    
}

#pragma mark ** 滑动tableView 的时候执行下面代码
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGPoint point = scrollView.contentOffset;
    
    if (point.y > 0) {
    
        self.imageViewForHeaderView.frame = CGRectMake(0,  -point.y -64, WIDTH,  WIDTH * 0.75);
        
        self.backview.frame = CGRectMake(0,0.35 * WIDTH -point.y, WIDTH, WIDTH * 0.4 - 64);
        
    } else {
        
        self.imageViewForHeaderView.frame = CGRectMake(0.5 * point.y /0.75 , -64, WIDTH - point.y /0.75 ,  WIDTH * 0.75 - point.y);
        
        self.backview.frame = CGRectMake( 0,0.35 * WIDTH - point.y, WIDTH , WIDTH * 0.4 - 64);
    }
    
}



@end
